package game.ball;

import com.uqbar.vainilla.DeltaState;
import com.uqbar.vainilla.appearances.Sprite;

public class Free extends SoccerBallState {
	
	private double versorI   = 0;
	private double versorJ   = 0;
	private double speed     = 0;
	private double acceleration = 0.01;
	
	private final int LIMIT_VECTOR    = 100;//pone un tope a la velocidad de los pases y los tiros al arco
	private final double DECELERATION = 0.2;
	
	public Free(SoccerBall ball) {
		this.setBall(ball);
		this.getBall().setRightAnimationWithSpeed(0.05 * (15 - this.getSpeed()));
	}

	public Free(SoccerBall ball, double speed, double i, double j) {
		this.setBall(ball);
		this.setSpeed(speed);
		
		while(i>LIMIT_VECTOR || j>LIMIT_VECTOR || i<-LIMIT_VECTOR || j<-LIMIT_VECTOR){
			i=i/2;
			j=j/2;
		}
		
		this.setVersorI(i);
		this.setVersorJ(j);
		this.getBall().setRightAnimationWithSpeed(0.05 * (15 - this.getSpeed()));
	}
	
	@Override
	public void update(DeltaState deltaState) {
		double advanced = this.getSpeed() * deltaState.getDelta();
		
		this.getBall().moveNormally(this.getVersorI() * advanced, this.getVersorJ() * advanced);
		
		if (this.getSpeed() > 0) {
			this.setSpeed(this.getSpeed() - DECELERATION);
		}
		else {
			this.setSpeed(0);
			this.getBall().setAppearance(Sprite.fromImage("/images/ball.png").crop(2, 2, 60, 60).scale(0.75));
		}
	}
	

	@Override
	protected void moveUpDownWithCentralizedCamera(double x, double y) {
		this.getBall().move(0, y);
		this.getBall().getScene().moveSprites(x, 0);
	}
	
	@Override
	protected void moveRightLeftWithCentralizedCamera(double x, double y) {
		this.getBall().move(x, 0);
		this.getBall().getScene().moveSprites(0, y);
	}

	@Override
	protected void advance(double x, double y) {
		this.getBall().move(x, y);
	}

	@Override
	public boolean isFree() {
		return true;
	}
	
	public void setVersorI(double i) {
		this.versorI = i;
	}

	public void setVersorJ(double j) {
		this.versorJ = j;
	}

	public void setSpeed(double speed) {
		this.speed = speed;
	}

	public double getVersorI() {
		return versorI;
	}

	public double getVersorJ() {
		return versorJ;
	}

	public double getAcceleration() {
		return acceleration;
	}

	public double getSpeed() {
		return speed;
	}
}
